﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ionic.Zip;
using RuneLog.AdventerersLog;
using RuneLog.Extensions;

namespace RuneLog.Stats
{
    public partial class StatsLookupForm : Form
    {
        private bool init = false;
        public StatsLookupForm()
        {
            InitializeComponent();

            dataView.Columns[0].Width = 80;
            dataView.Columns[1].Width = 80;
            dataView.SmallImageList = dataView.LargeImageList = RunescapeImageList.AsImageList;

            init = true;

            using (ZipFile zip = ZipFile.Read("resources/skills.zip"))
            {
                userImage.Image = Image.FromStream(zip["chat head.png"].OpenReader());
                clanImage.Image = Image.FromStream(zip["default banner.png"].OpenReader());
            }
        }

        private void lookupBtn_Click(object sender, EventArgs e)
        {
            rsnLabel.Text = usernameBox.Text.Trim();
            UserStats stats = null;
            try
            {
                stats = new UserStats(rsnLabel.Text, osCheck.Checked);
            }
            catch (UsernameIsNotMemberException ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            if (clanImage.WaitOnLoad)
                clanImage.CancelAsync();
            clanImage.LoadAsync(AdvlogLookupForm.GetClanBannerURL(rsnLabel.Text));

            if (userImage.WaitOnLoad)
                userImage.CancelAsync();
            userImage.LoadAsync(AdvlogLookupForm.GetFaceURL(rsnLabel.Text));

            Text = "Player Stats Lookup " + (osCheck.Checked ? "Oldschool " : "") + " - " + rsnLabel.Text;

            usernameBox.Clear();
            dataView.Items.Clear();
            int len = osCheck.Checked ? 24 : 27;
            for (int i = 0; i < len; i++)
            {
                Stat skill = (Stat)typeof(UserStats).GetProperty(Stat.GetByID(i)).GetValue(stats, null);
                ListViewItem lvi = new ListViewItem(skill.Name);
                lvi.SubItems.Add(skill.Level + "");
                lvi.ToolTipText = "Experience: " + skill.Experience.ToString("0,000") + "\r\nRank: " + skill.Rank.ToString("0,000");
                lvi.ImageIndex = RunescapeImageList.GetID(skill.Name);
                dataView.Items.Add(lvi);
            }
        }

        private void loadStats()
        {

        }
        private void dataView_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
        {
            if (init)
            {
                e.NewWidth = dataView.Columns[e.ColumnIndex].Width;
                e.Cancel = true;
            }
        }

        private void osCheck_CheckedChanged(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(rsnLabel.Text))
                return;
            UserStats stats = null;
            try
            {
                stats = new UserStats(rsnLabel.Text, osCheck.Checked);
            }
            catch (UsernameIsNotMemberException ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            Text = "Player Stats Lookup " + (osCheck.Checked ? "Oldschool " : "") + " - " + rsnLabel.Text;

            dataView.Items.Clear();
            int len = osCheck.Checked ? 24 : 27;
            for (int i = 0; i < len; i++)
            {
                Stat skill = (Stat)typeof(UserStats).GetProperty(Stat.GetByID(i)).GetValue(stats, null);
                ListViewItem lvi = new ListViewItem(skill.Name);
                lvi.SubItems.Add(skill.Level + "");
                lvi.ToolTipText = "Experience: " + skill.Experience.ToString("0,000") + "\r\nRank: " + skill.Rank.ToString("0,000");
                lvi.ImageIndex = RunescapeImageList.GetID(skill.Name);
                dataView.Items.Add(lvi);
            }
        }
    }
}
